package com.tcm.core.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.core.dozer.DozerConvert;
import com.tcm.core.domain.dto.HospitalDTO;
import com.tcm.core.domain.entity.Hospital;
import com.tcm.core.domain.enums.BusinessCodeEnum;
import com.tcm.core.mapper.HospitalMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService(version = "0.0.1-alpha",retries = 3, timeout = 6000, delay = -1)
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalMapper hospitalMapper;

    @DubboReference(version = "0.0.1-alpha",timeout = 6000, check = false,retries = 3)
    private ConfigService configService;

    @DubboReference(version = "0.0.1-beta",group = "cached-uid",timeout = 6000, check = false,retries = 3)
    private UidGenerator uidGenerator;

    /**
     * 获得医院信息
     *
     * @param id
     * @return Hospital
     */
    @Override
    public HospitalDTO findHospitalById(Long id) {
        Hospital hospital = hospitalMapper.findById(id);
        HospitalDTO hospitalDTO = null;
        if(ObjectUtil.isNotEmpty(hospital)) {
            hospitalDTO = DozerConvert.copyProperties(hospital, HospitalDTO.class);
        }
        return hospitalDTO;
    }

    /**
     * 新增医院信息
     *
     * @param hospitalDTO
     */
    @Override
    public void save(HospitalDTO hospitalDTO) throws ServiceException {
        if(ObjectUtil.isEmpty(hospitalDTO)){
            R.throwFail(BusinessCodeEnum.HOSPITAL_PARAMS_NOT_FOUND);
        }
        if(ObjectUtil.isEmpty(hospitalDTO.getName())){
            R.throwFail(BusinessCodeEnum.HOSPITAL_NAME_NOT_FOUND);
        }
        Hospital hospitalNames = hospitalMapper.findByName(hospitalDTO.getName());
        if(ObjectUtil.isNotEmpty(hospitalNames)){
            R.throwFail(BusinessCodeEnum.HOSPITAL_NAME_EXISTS);
        }
        Hospital hospital = new Hospital();
        BeanUtil.copyProperties(hospitalDTO, hospital);
        hospital.setId(uidGenerator.getUID());
        hospital.setCreateTime(LocalDateTime.now());
        hospital.setUpdateTime(LocalDateTime.now());
        hospital.setStatus(new Byte("0"));
        hospital.setDeleted(new Byte("0"));
        hospitalMapper.insert(hospital);
    }

    @Override
    public int update(HospitalDTO hospitalDTO) throws ServiceException {
        if(ObjectUtil.isEmpty(hospitalDTO)){
            R.throwFail(BusinessCodeEnum.HOSPITAL_PARAMS_NOT_FOUND);
        }
        if(ObjectUtil.isEmpty(hospitalDTO.getId())){
            R.throwFail(BusinessCodeEnum.HOSPITAL_ID_NOT_FOUND);
        }
        if(StringUtils.isEmpty(hospitalDTO.getName())){
            R.throwFail(BusinessCodeEnum.HOSPITAL_NAME_NOT_FOUND);
        }
        int count = hospitalMapper.checkName(hospitalDTO.getName(), hospitalDTO.getId());
        if(count > 0){
            R.throwFail(BusinessCodeEnum.HOSPITAL_NAME_EXISTS);
        }
        Hospital hospital = new Hospital();
        BeanUtil.copyProperties(hospitalDTO, Hospital.class);
        return hospitalMapper.update(hospital);
    }


    /**
     * 永久删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) throws ServiceException {
        return hospitalMapper.deleteById(id);
    }

    /**
     * 删除到回收站
     *
     * @param id
     * @return
     */
    @Override
    public int deleteLogic(Long id) throws ServiceException {
       return hospitalMapper.deleteLogicById(id);
    }

    /**
     * 获取分页数据
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<HospitalDTO> listHospitalPage(Long start, Long pagesize) throws ServiceException {
        Long count = hospitalMapper.countHospital();
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(new Long(count));
        if(count == 0){
            return new PageBean<>(null);
        }
        List<Hospital> hospitals = hospitalMapper.listHospitalPage(PageContext.getStart(), pagesize);
        List<HospitalDTO> hospitalDTOS = DozerConvert.copyList(hospitals, HospitalDTO.class);
        PageBean<HospitalDTO> hospitalDTOPageBean = new PageBean<>(hospitalDTOS);

        return hospitalDTOPageBean;
    }


}
